import Common from "../Common";
import ListsItem from "../common/lists/ListsItem";
import {bcSub} from "../common/Helpers";
import Page from "../common/Page";

const {ccclass, property} = cc._decorator;

@ccclass
export default class WithdrawRecords extends Common {

    @property(cc.Node)
    backBtn: cc.Node;

    @property(cc.Node)
    content: cc.Node;


    @property(cc.Node)
    page: cc.Node;

    itemPrefab: cc.Prefab


    protected bonusPage: any = {
        current_page: 1,
        last_page: 1,
        per_page: 0,
        total: 0,
    }

    protected bonusLists: any[]

    start() {
        cc.director.preloadScene('profile')
        this.initEmitter()
        this.getWithdrawRecords(1, false)
    }

    /**
     * 事件监听
     */
    protected initEmitter() {
        this.backBtn.on(cc.Node.EventType.TOUCH_START, () => {
            this.onBtnClick()
            cc.director.loadScene('profile')
        }, this)
        this.page.getComponent(Page).setCallback((page) => {
            this.getWithdrawRecords(page, false)
        })
    }

    async getWithdrawRecords(page = 1, isAppend = true) {
        try {
            const data = await this.http.get('withdraw/lists', {page})
            const lists = data.data || []
            if (isAppend) {
                this.bonusLists.push(...lists)
            } else {
                this.bonusLists = lists
            }
            this.bonusPage.current_page = data.current_page
            this.bonusPage.last_page = data.last_page
            this.bonusPage.per_page = data.per_page
            this.bonusPage.total = data.total
            this.page.getComponent(Page).init(data.current_page, data.last_page)
            this.initListItem(lists, isAppend)
        } catch (e) {

        }
    }

    protected async initListItem(lists = [], isAppend = true) {
        if (!isAppend) {
            this.content.removeAllChildren()
        }
        const itemPrefab = await this.getItemPrefab();
        lists.forEach(item => {
            const itemNode = cc.instantiate(itemPrefab)
            let color = '#999999', title = '待审核'
            if (item.status == 1) {
                color = '#5ACA00';
                title = '已提现'
            } else if (item.status == 2) {
                color = '#fea060';
                title = '已驳回'
            }
            itemNode.getComponent(ListsItem).init(title, [
                {label: '提现金额', value: item.apply_money},
                {label: '提现手续', value: bcSub(item.apply_money, item.money)},
                {label: '实际到账', value: item.money},
                {label: '提款方式', value: this.getWithdrawTypeName(item.withdraw_type)},
                {label: '提款账号', value: item.withdraw_account || ''},
            ], item.created_at, color)
            this.content.addChild(itemNode)
        })
    }

    protected getWithdrawTypeName(key) {
        if (!key) {
            return ''
        }
        const types = {
            weixin: '微信',
            alipay: '支付宝',
            bank: '银行卡',
        }
        return types[key] || ''
    }

    async getItemPrefab() {
        if (!this.itemPrefab) {
            this.itemPrefab = await this.getPrefab('lists/item')
        }
        return this.itemPrefab
    }

}
